package ru.scan2d.main.vizualizer.impl;

import java.awt.Adjustable;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import ru.scan2d.main.MainWindowPresenter.Listener;

public class VisualizerViewImpl extends JPanel {
	ArrayList<PicturePanelImpl> pictures = new ArrayList<PicturePanelImpl>();
	ArrayList<Integer> plotNumbers = new ArrayList<Integer>();
	ArrayList<Integer> picturesNumbers = new ArrayList<Integer>();
	ArrayList<JPanel> plots = new ArrayList<JPanel>();
	private JPanel controlsPanel;
	//private JPanel menuPanel;
	private JButton zoomIn;
	private JButton zoomOut;
	private javax.swing.JPanel content;
	private javax.swing.JScrollPane scrollPanel;

	private JPanel plotPanel;
	private JComboBox comboBox;
	//private JLabel zoomValue;
	private Listener listener;

	public VisualizerViewImpl() {
		initComponents();
		repaint();
		show();
	}

	public void setListener(Listener listener) {
		this.listener = listener;
	}

	@Deprecated
	public void addPicture(PicturePanelImpl picture) {
		pictures.add(picture);
		picture.setListener(listener);
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(pictures.get(0), BorderLayout.CENTER);
		panel.setSize(pictures.get(0).getImage().getWidth(), pictures.get(0)
				.getImage().getHeight());
		scrollPanel.setViewportView(panel);
		comboBox.addItem(pictures.size());
		comboBox.setSelectedIndex(0);
		repaint();
		show();
		picturesNumbers.add(comboBox.getItemCount());
	}

	public void addPicture(PicturePanelImpl picture, String name) {
		pictures.add(picture);
		scrollPanel.setViewportView(picture);
		comboBox.addItem(name == null ? pictures.size() : name);
		comboBox.setSelectedIndex(0);
		repaint();
		show();
		picturesNumbers.add(comboBox.getItemCount());
	}

	private void initComponents() {
		controlsPanel = new javax.swing.JPanel();
		comboBox = new JComboBox();
		comboBox.setSize(400, 25);
		Font font = new Font("sansserif", Font.BOLD, 32);
		zoomIn = new javax.swing.JButton();
		zoomOut = new javax.swing.JButton();
		scrollPanel = new javax.swing.JScrollPane();
		content = new javax.swing.JPanel();
		controlsPanel.add(comboBox);
		plotPanel = new JPanel();
		plotPanel.setSize(600, 600);
		controlsPanel.setName("controlsPanel");

		zoomIn.setText("+");
		zoomIn.setName("zoomIn");

		zoomOut.setText("-");
		zoomOut.setName("zoomOut");

		javax.swing.GroupLayout controlsPanelLayout = new javax.swing.GroupLayout(
				controlsPanel);
		controlsPanel.setLayout(controlsPanelLayout);
		controlsPanelLayout
				.setHorizontalGroup(controlsPanelLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								controlsPanelLayout
										.createSequentialGroup()
										.addContainerGap(281, Short.MAX_VALUE)
										.addComponent(
												zoomIn,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												73,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addComponent(
												zoomOut,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												73,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(11, 11, 11)));
		controlsPanelLayout
				.setVerticalGroup(controlsPanelLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								controlsPanelLayout
										.createSequentialGroup()
										.addGap(26, 26, 26)
										.addGroup(
												controlsPanelLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(zoomIn)
														.addComponent(zoomOut))
										.addContainerGap(19, Short.MAX_VALUE)));

		ActionListener comboBoxAction = new ActionListener() {
			public void actionPerformed(ActionEvent actionEvent) {
				System.out.println("INDEX " + comboBox.getSelectedIndex());
					plotPanel.setVisible(false);
					scrollPanel.setVisible(true);
					controlsPanel.setVisible(true);
					scrollPanel.setViewportView(pictures.get(comboBox.getSelectedIndex()));
					repaint();
					show();
					listener.needRepaint();
				}
		};
		comboBox.addActionListener(comboBoxAction);

		scrollPanel.setName("scrollPanel");

		content.setAutoscrolls(true);
		content.setMinimumSize(new java.awt.Dimension(150, 150));
		content.setName("content");

		javax.swing.GroupLayout contentLayout = new javax.swing.GroupLayout(
				content);
		content.setLayout(contentLayout);
		contentLayout.setHorizontalGroup(contentLayout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 446,
				Short.MAX_VALUE));
		contentLayout.setVerticalGroup(contentLayout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 448,
				Short.MAX_VALUE));

		scrollPanel.setViewportView(content);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(
														controlsPanel,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addGroup(
														javax.swing.GroupLayout.Alignment.TRAILING,
														layout.createSequentialGroup()
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.TRAILING)
																				.addComponent(
																						scrollPanel,
																						javax.swing.GroupLayout.Alignment.LEADING,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						438,
																						Short.MAX_VALUE)
																				.addComponent(
																						plotPanel,
																						javax.swing.GroupLayout.Alignment.LEADING,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						1000,
																						Short.MAX_VALUE)
																						)
																.addContainerGap()))));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(scrollPanel,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(plotPanel,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(controlsPanel,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)));

		AdjustmentListener scrollListener = new AdjustmentListener() {

			@Override
			public void adjustmentValueChanged(AdjustmentEvent arg0) {
				Adjustable adj = arg0.getAdjustable();
				try {
					if (adj.getOrientation() == Adjustable.HORIZONTAL) {
						if (pictures.size() != 0) {
							pictures.get(
									picturesNumbers.indexOf(comboBox
											.getSelectedIndex() + 1))
									.setScrollX(adj.getValue());
						}
					} else {
						if (pictures.size() != 0) {
							pictures.get(
									picturesNumbers.indexOf(comboBox
											.getSelectedIndex() + 1))
									.setScrollY(adj.getValue());
						}
					}
				} catch (Exception e) {
				}
			}
		};

		scrollPanel.getVerticalScrollBar()
				.addAdjustmentListener(scrollListener);
		scrollPanel.getHorizontalScrollBar().addAdjustmentListener(
				scrollListener);
		zoomIn.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				pictures.get(picturesNumbers.indexOf(comboBox.getSelectedIndex() + 1))
				.zoomIn();
			}
		});

		zoomOut.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				pictures.get(picturesNumbers.indexOf(comboBox.getSelectedIndex() + 1))
						.zoomOut();
			}
		});
	}

	public void clearSelectedPanels() {
		pictures = new ArrayList<PicturePanelImpl>();
		plotNumbers = new ArrayList<Integer>();
		picturesNumbers = new ArrayList<Integer>();
		plots = new ArrayList<JPanel>();
		initComponents();
		repaint();
		show();
		listener.needRepaint();
	}

	public BufferedImage getCurrentPicture() {
		return pictures.get(
				picturesNumbers.indexOf(comboBox.getSelectedIndex() + 1))
				.getImage();
	}
}